package com.zmzncs.lmtc.module.system.controller;


import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.util.JsonUtil;
import com.zmzncs.lmtc.common.util.RestUtil;
import com.zmzncs.lmtc.module.system.pojo.department.*;
import com.zmzncs.lmtc.module.system.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2020-10-12
 */
@Api(tags = "部门")
@RestController
@RequestMapping("/system/department")
public class DepartmentController {

    @Autowired
    DepartmentService departmentService;

    @ApiOperation(value = "根据部门id查询部门列表")
    @GetMapping(value = "get-by-id")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    public Rest getById(@RequestParam Long deptId) {
        Department department = departmentService.getById(deptId);
        DepartmentDTO departmentDTO = new DepartmentDTO();
        BeanUtils.copyProperties(department, departmentDTO);

        return RestUtil.success(department);
    }

    @ApiOperation(value = "查询部门列表")
    @GetMapping(value = "list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    public Rest<List<DepartmentVO>> list() {
        List<DepartmentDTO> departmentDTOList = departmentService.getDepartmentList();
        List<DepartmentVO> departmentVOList = JsonUtil.listTransform(departmentDTOList, DepartmentVO.class);

        return RestUtil.success(departmentVOList);
    }

    @ApiOperation(value = "添加部门")
    @PostMapping(value = "add")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    public Rest add(@RequestBody @Valid DepartmentAddForm form) {
        Department department = new Department();
        BeanUtils.copyProperties(form, department);

        departmentService.save(department);

        return RestUtil.success();
    }

    @ApiOperation(value = "修改部门")
    @PostMapping(value = "update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    public Rest update(@RequestBody @Valid DepartmentUpdateForm form) {
        Department department = new Department();
        BeanUtils.copyProperties(form, department);

        departmentService.updateById(department);

        return RestUtil.success();
    }

    @ApiOperation(value = "删除部门")
    @PostMapping(value = "delete/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    public Rest delete(@PathVariable Long id) {
        Department department = new Department();
        department.setId(id);
        department.setIsDeleted(1);
        departmentService.updateById(department);

        return RestUtil.success();
    }

}

